<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   allen.qiang
 */

quanli_require('module/base/goodsHandler.class.php');

class ajax_goods {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询商品信息
                    $this->searchGoodsDetail();
                    break;
				case 2: //查询销售商品列表
                    $this->searchGoodsList();
                    break;
                case 4: //商品的SKU
                    $this->goodsSkuInfo();
                    break;
                case 5:   //保存调价方法
                    $this->saveChangePrice();
                    break;
                case 6:   //编辑等级折扣
                    $this->editLevelDiscount();
                    break;
            }

        }

    }

    /**
     * 查询商品信息
     */
    private function searchGoodsDetail() {

        global $G_PGLOB;

        if (!isset($_REQUEST['product_code']) || empty($_REQUEST['product_code'])) {

            $result_array = array(
                'code' => 400,
                'message' => '商品编码不能为空'
            );

        } else {

            $search_sql = 'SELECT * FROM goods_sale WHERE product_code = ?';
            $goods_array = &$G_PGLOB->DBCA->stmt_query($search_sql, 's', $_REQUEST['product_code']);
            if ($goods_array) {
                $result_array = array(
                    'code' => 200,
                    'message' => 'OK',
                    'data' => array(
                    	'product_code' => $_REQUEST['product_code'],
                        'goods_name' => $goods_array[0]->goodsName,
                        'goods_price' => $goods_array[0]->salePrice,
                        'goodsMainPic' => $goods_array[0]->goodsMainPic
                    )
                );

            } else {

                $result_array = array(
                    'code' => 404,
                    'message' => '商品信息没有找到'
                );

            }

            echo json_encode($result_array);

        }

    }
    
    /**
     * 销售商品列表
     *
     */
    public function searchGoodsList (){
    	global $G_PGLOB;
        
		$goodsHandler = new goodsHandler( $G_PGLOB->DBCA ) ;
		
		//把三级商品编码转换成二级
        if (isset($_REQUEST['product_code']) && !empty($_REQUEST['product_code'])) {
        	$goods_sale_search = new entity('goods_sale');
        	$goods_sale_search->product_code = $_REQUEST['product_code'];
        	if ($G_PGLOB->DBCA->fetch($goods_sale_search)) {
        		$goods_list_search = new entity('goods_list');
        		$goods_list_search->postID = $goods_sale_search->twoPostID;
        		if ($G_PGLOB->DBCA->fetch($goods_list_search)) {
        			$_REQUEST['product_code'] = $goods_list_search->product_code;
        		}
        	}
        }
        
		$result_data  = $goodsHandler->searchGoodsList( $_REQUEST ) ;
		
		$preview_url = echo_preview_url();
		$publish_url = echo_publish_uri() ;

		$result_array = array(
            'page'  => $_REQUEST['page'] ,
            'total' => $_REQUEST['count'] ,
            'rows'  => array()
        );

		if ($result_data) {
			$edit_permission = $G_PGLOB->has_permission(524);
            foreach($result_data as &$g_l) {
            	
            	//商家编码
            	$goods_sale_code = new entity('goods_sale');
            	$goods_sale_code->twoPostID = $g_l['postID'];
            	if ($G_PGLOB->DBCA->fetch($goods_sale_code)) {
            		$g_l['product_code'] = $goods_sale_code->product_code;
            	} else {
            		$g_l['product_code'] = '';
            	}
            	
            	if($edit_permission){
	        		$operating = '<a href="javascript: void(0);" onclick="goods.details(' . $g_l['postID'] . ')">详情</a>&nbsp;&nbsp;' .
            		'<a href="javascript: void(0);" onclick="goods.publish(' . $g_l['uid'] . ' , \'' . $publish_url . '\')">发布</a>&nbsp;&nbsp;' .
            		'<a href="javascript: void(0);" onclick="goods.preview(' . $g_l['uid'] . ', \'' . $preview_url . '\')">预览</a>';
            	}else{
            		$operating = '<a href="javascript: void(0);" onclick="goods.details(' . $g_l['postID'] . ')">详情</a>&nbsp;&nbsp;' .
            		'<a href="javascript: void(0);" onclick="goods.preview(' . $g_l['uid'] . ', \'' . $preview_url . '\')">预览</a>';
            	}
            	
            	$result_array['rows'][] = array(
                    'id'   => $g_l['postID'],
                    'cell' => array(
            			$operating,
            			$g_l['product_code'], 
            			'<font title="' . $g_l['goodsName'] . '"> <a href="/shop/goods-' . $g_l['uid'] . '.html" target="_blank">' . $g_l['goodsName'] . '</a> <font/>',
            			$g_l['bigCategoryName'], 
            			$g_l['midCategoryName'], 
            			$g_l['smallCategoryName'], 
            			'<font color="red">' . $g_l['marketPrice'] . '<font/>',
            			'<font color="red">' . $g_l['salePrice'] . '<font/>'
					),
                );
            }
        }

        echo json_encode( $result_array );
    }

    
    /**
     * 商品SKU信息
     *
     */
    private function goodsSkuInfo() {
    	global $G_PGLOB;
    	
    	//查询商品SKU信息
    	$commHandler = new commHandler( $G_PGLOB->DBCA ) ;

    	$arg_array = array(
    		'twoPostID' => $_REQUEST['twoPostID'],
			'sortname' 	=> 'spec' , 
			'sortorder' => 'asc' , 
			'page' 		=> '0' , 
			'rp' 		=> '50' 
		);
		$result_data = $commHandler->search( $arg_array , 'goods_sale' , '' ) ;
		
		//结果处理
		$result_array = array(
            'page'  => $arg_array['page'] ,
            'total' => $arg_array['count'] ,
            'rows'  => array()
        );
        
		if ($result_data) {
            foreach($result_data as &$g_l) {
        		$result_array['rows'][] = array(
                    'id'   => $g_l->postID,
                    'cell' => array(
	                    			$g_l->product_code,
	                    			$g_l->goodsName ,
	                    			$g_l->spec ,
	                    			$g_l->marketPrice,
	                    			$g_l->salePrice, 
	                    			$g_l->addPoints, 
	                    			$g_l->salePoints, 
	                    			$g_l->useFlg==1?'有':'无'),
                    
                );
            }
        }
		
		echo json_encode($result_array);
    }

    //保存调价方法
    function saveChangePrice(){

        global $G_PGLOB;

        //实例化公共商品类
        $goodsHandler = new goodsHandler( $G_PGLOB->DBCA );

        //调用调价方法
        $_REQUEST['creator'] = $G_PGLOB->currentUserID ;
        $result = $goodsHandler->changeGoodsPrice( $_REQUEST , 'goods_list' , 'goods_sale' );

        echo json_encode( $result );
        return;

    }

    //编辑等级折扣
    function editLevelDiscount(){

        global $G_PGLOB;

        //实例化公共商品类
        $goodsHandler = new goodsHandler( $G_PGLOB->DBCA );

        //接收参数
        $postID = $_REQUEST['postID'];
        $changeLevel = $_REQUEST['changeLevel'];

        if( $changeLevel == 1 ){
            $discountFlg = 1;
        }else if (  $changeLevel == 2 ){
            $discountFlg = 0;
        }else if(  $changeLevel == 3 ){
            $discountFlg = substr($_REQUEST['saveString'],1);
        }

        //调用编辑等级折扣方法
        $result = $goodsHandler->editLevelDiscount( $postID , $discountFlg , 'goods_list' , 'goods_sale' );

        echo json_encode( $result );
        return;

    }


}
